package A一周刷爆LeetCode.A基础班.G图;

/**
 * @author wei.zhao
 * @description:
 * @date 2022年03月10日 10:09
 */
public class GraphGenerator {

    public static Graph createGraph(Integer[][] matrix) {
        Graph graph = new Graph();
        for (int i = 0; i < matrix.length; i++) {
            Integer[] integers = matrix[i];
            Integer weight = integers[0];
            Integer from = integers[1];
            Integer to = integers[2];
            if (graph.nodes.get(from) == null) {
                graph.nodes.put(from, new Node(from));
            }
            if (graph.nodes.get(to) == null) {
                graph.nodes.put(to, new Node(to));
            }
            Node fromNode = graph.nodes.get(from);
            Node toNode = graph.nodes.get(to);
            Edge newEdge = new Edge(weight, fromNode, toNode);
            fromNode.nexts.add(toNode);
            fromNode.out++;
            fromNode.edges.add(newEdge);
            toNode.in++;
            graph.edges.add(newEdge);
        }
        return graph;
    }

}
